package com.fuchuang.seckillsystem.utils;

import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 本系统全部使用sm4加密方式
 */
@Component
public class Sm4Utils {
    
    @Value("${sm4.key}")
    private String key;
    
    
    public String encrypt(String content) {
        SymmetricCrypto crypto = SmUtil.sm4(key.getBytes(StandardCharsets.UTF_8));
        return crypto.encryptHex(content);
    }
    
    public Boolean match(String raw, String encrypt) {
        String result = this.encrypt(raw);
        return result.equals(encrypt);
    }
    
}
